package com.fsi.client;

import java.util.List;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class TestGUI extends ServiceGUI {
	private final Label title = new Label("Test Page");
	private final TextArea customerList = new TextArea();
	private final HorizontalPanel entryPanel = new HorizontalPanel();
	private final TextBox newNameBox = new TextBox();
	protected final Button addButton = new Button("Add Name");
	protected final Button deleteCustomerButton = new Button("Delete");
	private final Label status;

	public TestGUI() {
		this.add(title);
		status = new Label("");
		customerList.setReadOnly(true);
		customerList.addStyleName("textArea");

		entryPanel.add(newNameBox);
		entryPanel.add(addButton);
		entryPanel.add(deleteCustomerButton);

		this.add(entryPanel);
		this.add(customerList);
		this.add(status);

	}

	// ***************************************
	// *********************** general methods
	// ***************************************
	public void resetNameBox() {
		newNameBox.setText("");
		newNameBox.setFocus(true);
		addButton.setEnabled(true);
	};

	public void listCustomers() {
		service.listCustomers(this);
	}

	public void addNewCustomer() {
		String tempName = newNameBox.getText().trim();
		if (tempName.equals("")) {
			resetNameBox();
			return;
		}
		addButton.setEnabled(false);
		service.addCustomer(this, new Customer(tempName));
	}

	public void deleteCustomer() {
		String name = newNameBox.getText().trim();
		Customer currentCustomer;
		if (name.equals("")) {
			resetNameBox();
			return;
		}
		currentCustomer = getCustomerByName(name);
		if (currentCustomer != null) {
			newNameBox.setText("");
			newNameBox.setFocus(true);
			service.deleteCustomer(this, currentCustomer);
		}
	}

	private Customer getCustomerByName(String name) {
		Customer result = null;
		List<Customer> customers = gui.customers;
		for (Customer c : customers) {
			if (c.getCustomerName().compareToIgnoreCase(name) == 0) {
				result = c;
			}
		}
		return result;

	}

	// ****************************
	// ************ service methods
	// ****************************
	@Override
	public void service_ListRetrievedFromService(List<Customer> result) {
		gui.customers = result;
		StringBuffer s = new StringBuffer();
		for (Customer c : result) {
			s.append(c.getUserName() + ": " + c.getLoginEmailAddress());
			s.append("\n");
		}
		customerList.setText(s.toString());
	};

	@Override
	public void service_ListContactsFailed(Throwable caught) {
		customerList.setText(caught.getMessage());
	};

	@Override
	public void service_addFailed(String errorMsg) {
		customerList.setText(errorMsg);
		resetNameBox();
	}

	@Override
	public void service_addSucceeded(Customer customer) {
		listCustomers();
		resetNameBox();
	}

	@Override
	public void service_deleteFailed(Throwable caught) {
		customerList.setText(caught.getMessage());
	}

	@Override
	public void service_deleteSucceeded() {
		service.listCustomers(this);
		resetNameBox();
	};

	// public void addNewCustomer() {
	// String tempName = newNameBox.getText().trim();
	// if (tempName.equals(""))
	// return;
	// newCustomerButton.setEnabled(false);
	// try {
	// service.addCustomer(new Customer(tempName));
	// } catch (Exception e) {
	// // status.setText(e.getMessage());
	// Window.alert(e.getMessage());
	// }
	// }
	//
	// @Override
	// public void service_ListContactsFailed(Throwable caught) {
	// // TODO Auto-generated method stub
	// status.setText("Unable to get customers for test");
	// }
	//
	// @Override
	// public void service_ListRetrievedFromService(List<Customer> result) {
	// // TODO Auto-generated method stub
	// status.setText("Retrieved customer list for test");
	// gui.customers = result;
	// this.textArea.setText("");
	//
	// StringBuffer s = new StringBuffer();
	// for (Customer c : result) {
	// s.append(c.getCustomerName());
	// s.append("\n");
	// }
	// textArea.setText(s.toString());
	// }
	//
	// public void service_addSucceeded(Customer customer) {
	// newNameBox.setText("");
	// newNameBox.setFocus(true);
	// status.setText("Customer added: " + customer.getCustomerName());
	// newCustomerButton.setEnabled(true);
	// service.listCustomers(this);
	// }
	//

}
